from elasticsearch import Elasticsearch
from app.logger import get_logger
import ssl
logger = get_logger('es_client')

class ESClient:
    def __init__(self, host, port, scheme='http', username=None, password=None, verify_certs=True):
        es_kwargs = {
            'hosts': [{
                'host': host,
                'port': int(port),
                'scheme': scheme
            }],
            'verify_certs': verify_certs,
            'ssl_show_warn': False,
            'http_auth': (username, password) if username and password else None
        }

        self.client = Elasticsearch(**es_kwargs)
        logger.info(f"初始化ESClient: {host}:{port} {scheme} verify_certs={verify_certs}")

    def ping(self):
        result = self.client.ping()
        logger.info(f"ES ping: {result}")
        return result

    def search(self, *args, **kwargs):
        return self.client.search(*args, **kwargs) 